package lab4;

import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class CourseSelection extends Thread{
    private final List<String> courses;
    private final Condition notFull, notEmpty;
    final Lock lock;

    public CourseSelection(List<String> courses, Condition notFull, Condition notEmpty, Lock lock){
        this.courses = courses;
        this.notFull = notFull;
        this.notEmpty = notEmpty;
        this.lock = lock;
    }

    @Override
    public void run() {
        while (true){
            lock.lock();
            if (courses.size() == 0){
                try {
                    notEmpty.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }else{
                String c = courses.remove(0);
                System.out.println("已处理学生" + c + "的请求。。。");
                notFull.signalAll();
                this.yield();
            }
            lock.unlock();
        }
    }
}
